/**
  ************************************* Copyright ******************************   
  *                 (C) Copyright 2019; CWL; China;  GCU.
  *                            All Rights Reserved
  *                              
  *                     By(武汉市先讯科技有限公司)
  *                     https://www.sensertek.com
  *      
  * FileName   : hal_timer.h   
  * Version    : v1.0		
  * Author     : CWL			
  * Date       : 2019-05-07         
  * Description:    
  * Function List:  
  	1. ....
  	   <version>: 		
  <modify staff>:
  		  <data>:
   <description>:  
  	2. ...
  ******************************************************************************
 */



#ifndef __HAL_TIMER_H_
#define __HAL_TIMER_H_

#include "hal_mcu.h"
#include "hal_types.h"

//================定时器0配置====================
#define		Timer0_16bitAutoReload()	CLEAR_BIT_MASK(TMOD, 0x03)			/* 16位自动重装	*/
#define		Timer0_16bit()			    MODIFY_REG(TMOD, 0x03, 0x01)	/* 16位*/
#define		Timer0_8bitAutoReload()		MODIFY_REG(TMOD, 0x03, 0x02)	/* 8位自动重装	*/
#define		Timer0_16bitAutoRL_NoMask()	SET_BIT_MASK(TMOD, 0x03)      		/* 16位自动重装不可屏蔽中断*/
#define 	Timer0_Run()	 		    TR0 = 1			                /* 允许定时器0计数*/
#define 	Timer0_Stop()	 		    TR0 = 0			                /* 禁止定时器0计数*/
#define		Timer0_Gate_INT0_P32()		SET_BIT_MASK(TMOD, 0x08)		        /* 时器0由外部INT0高电平允许定时计数 */
#define		Timer0_AsTimer()		    CLEAR_BIT_MASK(TMOD, 0x04)	        /* 时器0用做定时器*/
#define		Timer0_AsCounter()		    SET_BIT_MASK(TMOD, 0x04)		/* 时器0用做计数器*/
#define		Timer0_AsCounterP34()		SET_BIT_MASK(TMOD, 0x04)		/* 时器0用做计数器*/
#define 	Timer0_1T()			        SET_BIT_MASK(AUXR, 0x80)		/* Timer0 clodk = fo*/
#define 	Timer0_12T()			    CLEAR_BIT_MASK(AUXR, 0x80)		/* Timer0 clodk = fo/12	12分频; 	default	*/
#define		Timer0_CLKO_Enable()		SET_BIT_MASK(INTCLKO, 1)		/* 允许 T0 溢出脉冲在T0(P3.5)脚输出，Fck0 = 1/2 T0 溢出率，T0可以1T或12T。*/
#define		Timer0_CLKO_Disable()		CLEAR_BIT_MASK(INTCLKO, 1)
#define 	Timer0_InterruptEnable()	ET0 = 1			/* 允许Timer1中断.*/
#define 	Timer0_InterruptDisable()	ET0 = 0			/* 禁止Timer1中断.*/

#define		T0_Load(n)			        st( TH0 = (n) / 256; TL0 = (n) % 256; )
#define		T0_Load_us_1T(n)		    st( Timer0_AsTimer();Timer0_1T(); Timer0_16bitAutoReload();TH0=(65536-((MAIN_Fosc/1000)*(n)+00)/1000)/256;  TL0=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)%256; )
#define		T0_Load_us_12T(n)		    st( Timer0_AsTimer(); Timer0_12T(); Timer0_16bitAutoReload(); TH0=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)/256; TL0=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)%256; )
#define		T0_Frequency_1T_P35(n)		st( ET0=0; Timer0_AsTimer(); Timer0_1T(); Timer0_16bitAutoReload(); TH0=(65536-(n/2+MAIN_Fosc/2)/(n))/256; TL0=(65536-(n/2+MAIN_Fosc/2)/(n))%256; Timer0_CLKO_Enable(); TR0=1; )     /* fx=fosc/(2*M)/n;   M=1 or M=12 */
#define		T0_Frequency_12T_P35(n)		st( ET0=0; Timer0_AsTimer(); Timer0_12T(); Timer0_16bitAutoReload(); TH0=(65536-(n/2+MAIN_Fosc/24)/(n))/256; TL0=(65536-(n/2+MAIN_Fosc/24)/(n))%256; Timer0_CLKO_Enable(); TR0=1; )	/* fx=fosc/(2*M)/n;   M=1 or M=12 */

//================定时器1配置====================
#define		Timer1_16bitAutoReload()	CLEAR_BIT_MASK(TMOD, 0x30)		/* 16位自动重装	*/
#define		Timer1_16bit()			    MODIFY_REG(TMOD, 0x30, 0x10)	/* 16位	*/
#define		Timer1_8bitAutoReload()		MODIFY_REG(TMOD, 0x30, 0x20)	/* 8位自动重装	*/
#define 	Timer1_Run()	 		    TR1 = 1			/* 允许定时器1计数*/
#define 	Timer1_Stop()	 		    TR1 = 0			/* 禁止定时器1计数*/
#define		Timer1_Gate_INT1_P33()		SET_BIT_MASK(TMOD, 0x80)		/* 时器1由外部INT1高电平允许定时计数*/
#define		Timer1_AsTimer()		    CLEAR_BIT_MASK(TMOD, 0xC0)		/* 时器1用做定时器*/
#define		Timer1_AsCounter()		    SET_BIT_MASK(TMOD, 0x40)		/* 时器1用做计数器*/
#define		Timer1_AsCounterP35()		SET_BIT_MASK(TMOD, 0x40)		/* 时器1用做计数器*/
#define 	Timer1_1T()			        SET_BIT_MASK(AUXR, 0x40)		/* Timer1 clodk = fo*/
#define 	Timer1_12T()			    CLEAR_BIT_MASK(AUXR, 0x40)		/* Timer1 clodk = fo/12	12分频; 	default	*/
#define		Timer1_CLKO_Enable()		SET_BIT_MASK(INTCLKO, 0x02)		/* 允许 T1 溢出脉冲在T1(P3.4)脚输出，Fck1 = 1/2 T1 溢出率，T1可以1T或12T。*/
#define		Timer1_CLKO_Disable()		CLEAR_BIT_MASK(INTCLKO, 0x02)
#define     Timer1_CLKO_Enable_P34()    SET_BIT_MASK(INTCLKO, 0x02)		/* 允许 T1 溢出脉冲在T1(P3.4)脚输出，Fck1 = 1/2 T1 溢出率，T1可以1T或12T。*/
#define     Timer1_CLKO_Disable_P34()   CLEAR_BIT_MASK(INTCLKO, 0x02)
#define 	Timer1_InterruptEnable()	ET1 = 1			/* 允许Timer1中断.*/
#define 	Timer1_InterruptDisable()	ET1 = 0			/* 禁止Timer1中断.*/

#define		T1_Load(n)			        st( TH1 = (n) / 256; TL1 = (n) % 256;
#define		T1_Load_us_1T(n)		    st( Timer1_AsTimer(); Timer1_1T();  Timer1_16bitAutoReload(); TH1=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)/256;  TL1=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)%256; )
#define		T1_Load_us_12T(n)		    st( Timer1_AsTimer(); Timer1_12T(); Timer1_16bitAutoReload(); TH1=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)/256; TL1=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)%256; )
#define		T1_Frequency_1T_P34(n)		st( ET1=0; Timer1_AsTimer(); Timer1_1T(); Timer1_16bitAutoReload(); TH1=(65536-(n/2+MAIN_Fosc/2)/(n))/256; TL1=(65536-(n/2+MAIN_Fosc/2)/(n))%256; Timer1_CLKO_Enable_P34(); TR1=1; )	/* fx=fosc/(2*M)/n;   M=1 or M=12 */
#define		T1_Frequency_12T_P34(n)		st( ET1=0; Timer1_AsTimer(); Timer1_12T(); Timer1_16bitAutoReload(); TH1=(65536-(n/2+MAIN_Fosc/24)/(n))/256; TL1=(65536-(n/2+MAIN_Fosc/24)/(n))%256; Timer1_CLKO_Enable_P34(); TR1=1; )	/* fx=fosc/(2*M)/n;   M=1 or M=12 */

//================定时器2配置====================
#define 	Timer2_Run()	 		    AUXR |=  (1<<4)	/* 允许定时器2计数*/
#define 	Timer2_Stop()	 		    AUXR &= ~(1<<4)	/* 禁止定时器2计数*/
#define		Timer2_AsTimer()		    AUXR &= ~(1<<3)	/* 时器2用做定时器*/
#define		Timer2_AsCounter()		    AUXR |=  (1<<3)	/* 时器2用做计数器*/
#define		Timer2_AsCounterP31()		AUXR |=  (1<<3)	/* 时器2用做计数器*/
#define 	Timer2_1T()			        AUXR |=  (1<<2)	/* Timer0 clock = fo*/
#define 	Timer2_12T()			    AUXR &= ~(1<<2)	/* Timer0 clock = fo/12	12分频; 	default	*/
#define		Timer2_CLKO_Enable()		SET_BIT_MASK(INTCLKO, 0x04)	/* 允许 T2 溢出脉冲在T1(P3.0)脚输出，Fck2 = 1/2 T2 溢出率，T2可以1T或12T。*/
#define		Timer2_CLKO_Disable()		CLEAR_BIT_MASK(INTCLKO, 0x04)
#define 	Timer2_InterruptEnable()	IE2  |=  (1<<2)		/* 允许Timer2中断.	*/
#define 	Timer2_InterruptDisable()	IE2  &= ~(1<<2)		/* 禁止Timer2中断.	*/

#define		T2_Load(n)			        TH2 = (n) / 256; TL2 = (n) % 256
#define		T2_Load_us_1T(n)		    Timer2_AsTimer(); Timer2_1T();  TH2=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)/256;  TL2=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)%256
#define		T2_Load_us_12T(n)		    Timer2_AsTimer(); Timer2_12T(); TH2=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)/256; TL2=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)%256
#define		T2_Frequency_1T_P13(n)		Timer2_InterruptDisable(); Timer2_AsTimer(); Timer2_1T();  TH2=(65536-(n/2+MAIN_Fosc/2)/(n))/256;  TL2=(65536-(n/2+MAIN_Fosc/2)/(n))%256;  Timer2_CLKO_Enable(); Timer2_Run()	/* fx=fosc/(2*M)/n;   M=1 or M=12 */
#define		T2_Frequency_12T_P13(n)		Timer2_InterruptDisable(); Timer2_AsTimer(); Timer2_12T(); TH2=(65536-(n/2+MAIN_Fosc/24)/(n))/256; TL2=(65536-(n/2+MAIN_Fosc/24)/(n))%256; Timer2_CLKO_Enable(); Timer2_Run()	/* fx=fosc/(2*M)/n;   M=1 or M=12 */

//================定时器3配置====================
#define 	Timer3_Run()	 		    T4T3M |=  (1<<3)	/* 允许定时器3计数*/
#define 	Timer3_Stop()	 		    T4T3M &= ~(1<<3)	/* 禁止定时器3计数*/
#define		Timer3_AsTimer()		    T4T3M &= ~(1<<2)	/* 时器3用做定时器*/
#define		Timer3_AsCounter()		    T4T3M |=  (1<<2)	/* 时器3用做计数器;  P0.5为外部脉冲*/
#define		Timer3_AsCounterP05()		T4T3M |=  (1<<2)	/* 时器3用做计数器;  P0.5为外部脉冲*/
#define 	Timer3_1T()					T4T3M |=  (1<<1)	/* 1T模式*/
#define 	Timer3_12T()				T4T3M &= ~(1<<1)	/* 12T模式; default*/
#define		Timer3_CLKO_Enable()		T4T3M |=  1		/* 允许T3溢出脉冲在T3(P0.5)脚输出，Fck = 1/2 T2 溢出率，T2可以1T或12T。	*/
#define		Timer3_CLKO_Disable()		T4T3M &= ~1		/* 禁止T3溢出脉冲在T3(P0.5)脚输出*/
#define		Timer3_CLKO_Enable_P05()	T4T3M |=  1		/* 允许T3溢出脉冲在T3(P0.5)脚输出，Fck = 1/2 T2 溢出率，T2可以1T或12T。	*/
#define		Timer3_CLKO_Disable_P05()	T4T3M &= ~1		/* 禁止T3溢出脉冲在T3(P0.5)脚输出*/
#define 	Timer3_InterruptEnable()	IE2  |=  (1<<5)		/* 允许Timer3中断.*/
#define 	Timer3_InterruptDisable()	IE2  &= ~(1<<5)		/* 禁止Timer3中断.*/

#define		T3_Load(n)					st( TH3 = (n) / 256; TL3 = (n) % 256; )
#define		T3_Load_us_1T(n)		    st( Timer3_AsTimer(); Timer3_1T(); TH3=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)/256; TL3=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)%256; )
#define		T3_Load_us_12T(n)		    st( Timer3_AsTimer(); Timer3_12T(); TH3=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)/256; TL3=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)%256; )
#define		T3_Frequency_1T_P05(n)		st( Timer3_InterruptDisable(); Timer3_AsTimer(); Timer3_1T(); TH3=(65536-(n/2+MAIN_Fosc/2)/(n))/256; TL3=(65536-(n/2+MAIN_Fosc/2)/(n))%256; Timer3_CLKO_Enable_P05(); Timer3_Run(); )	/* fx=fosc/(2*M)/n;   M=1 or M=12 */
#define		T3_Frequency_12T_P05(n)		st( Timer3_InterruptDisable(); Timer3_AsTimer(); Timer3_12T(); TH3=(65536-(n/2+MAIN_Fosc/24)/(n))/256; TL3=(65536-(n/2+MAIN_Fosc/24)/(n))%256; Timer3_CLKO_Enable_P05(); Timer3_Run(); )	/* fx=fosc/(2*M)/n;   M=1 or M=12 */

//================定时器4配置====================
#define 	Timer4_Run()	 		    T4T3M |=  (1<<7)	/* 允许定时器4计数*/
#define 	Timer4_Stop()	 		    T4T3M &= ~(1<<7)	/* 禁止定时器4计数*/
#define		Timer4_AsTimer()		    T4T3M &= ~(1<<6)	/* 时器4用做定时器*/
#define		Timer4_AsCounter()		    T4T3M |=  (1<<6)	/* 时器4用做计数器;  P0.7为外部脉冲*/
#define		Timer4_AsCounterP07()		T4T3M |=  (1<<6)	/* 时器4用做计数器;  P0.7为外部脉冲*/
#define 	Timer4_1T()			        T4T3M |=  (1<<5)	/* 1T模式*/
#define 	Timer4_12T()			    T4T3M &= ~(1<<5)	/* 12T模式; default*/
#define		Timer4_CLKO_Enable()		T4T3M |=  (1<<4)	/* 允许T4溢出脉冲在T4(P0.7)脚输出，Fck = 1/2 T2 溢出率，T2可以1T或12T。	*/
#define		Timer4_CLKO_Disable()		T4T3M &= ~(1<<4)	/* 禁止T4溢出脉冲在T4(P0.7)脚输出*/
#define		Timer4_CLKO_Enable_P07()	T4T3M |=  (1<<4)	/* 允许T4溢出脉冲在T4(P0.7)脚输出，Fck = 1/2 T2 溢出率，T2可以1T或12T。	*/
#define		Timer4_CLKO_Disable_P07()	T4T3M &= ~(1<<4)	/* 禁止T4溢出脉冲在T4(P0.7)脚输出*/
#define 	Timer4_InterruptEnable()	IE2  |=  (1<<6)		/* 允许Timer4中断.*/
#define 	Timer4_InterruptDisable()	IE2  &= ~(1<<6)		/* 禁止Timer4中断.*/

#define		T4_Load(n)			        st( TH4 = (n) / 256; TL4 = (n) % 256; )
#define		T4_Load_us_1T(n)		    st( Timer4_AsTimer(); Timer4_1T(); TH4=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)/256; TL4=(65536-((MAIN_Fosc/1000)*(n)+500)/1000)%256; )
#define		T4_Load_us_12T(n)		    st( Timer4_AsTimer(); Timer4_12T(); TH4=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)/256; TL4=(65536-((MAIN_Fosc/12000)*(n)+500)/1000)%256; )
#define		T4_Frequency_1T_P07(n)		st( Timer4_InterruptDisable(); Timer4_AsTimer(); Timer4_1T(); TH4=(65536-(n/2+MAIN_Fosc/2)/(n))/256; TL4=(65536-(n/2+MAIN_Fosc/2)/(n))%256; Timer4_CLKO_Enable_P07();Timer4_Run(); )	/* fx=fosc/(2*M)/n;   M=1 or M=12 */
#define		T4_Frequency_12T_P07(n)		st( Timer4_InterruptDisable(); Timer4_AsTimer(); Timer4_12T();TH4=(65536-(n/2+MAIN_Fosc/24)/(n))/256; TL4=(65536-(n/2+MAIN_Fosc/24)/(n))%256; Timer4_CLKO_Enable_P07();Timer4_Run(); )	/* fx=fosc/(2*M)/n;   M=1 or M=12 */

void HalTimer0Init(void);
uint16 ll_McuPrecisionCount(void);

#endif


 

